//
//  MainTabbarViewController.m
//  GaimeiVisitor
//
//  Created by ypwl on 15/6/26.
//  Copyright (c) 2015年 ypwl. All rights reserved.
//

#import "MainTabbarViewController.h"
#import "BaseNavigationController.h"
#import <objc/runtime.h>
#import "CALayer+Anim.h"

@interface MainTabbarViewController ()<UITabBarControllerDelegate>{
    
    
}
@property (strong, nonatomic) NSDate *lastPlaySoundDate;

@end

@implementation MainTabbarViewController{
}

-(id)init{
    
    self=[super init];
    if (self) {
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeIconAction:) name:ChangeTabbarIconNotification object:nil];
    }
    return self;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.delegate=self;
    
    self.recommendVC = [[YPRecommendViewController alloc]init];
    
    self.investVC = [[YPInvestViewController alloc]init];
    
    self.assetsVC = [[YPAssetsViewController alloc]init];
    
    self.discoverVC = [[YPDiscoverViewController alloc]init];
    
    [self addChildVC:self.recommendVC title:@"首页"];
    
    [self addChildVC:self.investVC title:@"理财"];
    
    [self addChildVC:self.discoverVC title:@"发现"];
    
    [self addChildVC:self.assetsVC title:@"我的"];
    
    [self setIconWithIndex:3];
    
}

- (void)changeIconAction:(NSNotification *)notif{
    
    NSInteger index = [notif.object integerValue];
    if (index != 0 && index < 4) {
        
        [self setIconWithIndex:[notif.object integerValue]];
    }
}

- (void)setIconWithIndex:(NSInteger)index{
    
    NSArray *homeImagesW =     @[@"sy-x",@"xinnian_w",@"gui_w",@"yin_w"];
    NSArray *homeImagesY =     @[@"sy-w",@"xinnian_x",@"gui_x",@"yin_y"];
    
    NSArray *investImagesW =   @[@"tz-w",@"chun_w",@"xin_w",@"hang_w"];
    NSArray *investImagesY =   @[@"tz-x",@"chun_x",@"xin_x",@"hang_y"];
    
    NSArray *discoverImagesW = @[@"fx-w",@"ji_w",@"si_w",@"cun_w"];
    NSArray *discoverImagesY = @[@"fx-x",@"ji_x",@"si_x",@"cun_y"];
    
    NSArray *myImagesW =       @[@"wd-w",@"xiang_w",@"jian_w",@"guan_w"];
    NSArray *myImagesY =       @[@"wd-x",@"xiang_x",@"jian_x",@"guan_y"];
    
    [self setImage:homeImagesW[index] selectImage:homeImagesY[index] vc:self.recommendVC];
    [self setImage:investImagesW[index] selectImage:investImagesY[index] vc:self.investVC];
    [self setImage:discoverImagesW[index] selectImage:discoverImagesY[index] vc:self.discoverVC];
    [self setImage:myImagesW[index] selectImage:myImagesY[index] vc:self.assetsVC];
    
    
}

- (void)setImage:(NSString *)imageStr selectImage:(NSString *)selectImageStr vc:(UIViewController *)vc{
    
    vc.tabBarItem.image = [[UIImage imageNamed:imageStr] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageStr] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

-(void)addChildVC:(UIViewController *)vc title:(NSString *)title{
    
    vc.title = title;
    
    
    //     设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = UIColorFromString(@"#0078e0");
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    
    
    selectTextAttrs[NSForegroundColorAttributeName] = UIColorFromString(@"#0078e0");
    [vc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [vc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:vc];
    [self addChildViewController:nav];
    
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
    UIView * tabBarButton = [viewController.tabBarItem valueForKey:@"view"];
    [tabBarButton.layer scale];
    
}


@end
